package com.sanqing.action;

import java.io.File;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.sanqing.base.action.BaseAction;
import com.sanqing.po.Course;
import com.sanqing.po.Subject;
import com.sanqing.service.CourseService;
import com.sanqing.service.SubjectService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;

@Results({ 
	   @Result(name = "list", location = "/teacher/subject/subject-list.jsp"), 
	   @Result(name = "input", location = "/teacher/subject/subject-add.jsp"), 
	   @Result(name = "inputBatch", location = "/teacher/subject/subject-addBatch.jsp"), 
	   @Result(name = "edit", location = "/teacher/subject/subject-edit.jsp"), 
	   @Result(name = "view", location = "/teacher/subject/subject-view.jsp"),
	   @Result(name = "success", location = "subject.action", type = "redirect")

})
public class SubjectAction extends BaseAction<Subject> {

	private static final long serialVersionUID = -8060701771704877996L;
	private int currentPage;
	private Subject subject = new Subject();
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private CourseService courseService;

	private String myFileContentType;
	private String myFileFileName;
	private File myFile;

	/**
	 * 查询试题信息
	 */
	@Override
	public String list() throws Exception {
		Page page = new Page();
		page.setEveryPage(10);// 每页显示10条记录
		page.setCurrentPage(currentPage);// 设置当前页
		PageResult pageResult = subjectService.querySubjectByPage(page, subject);
		getRequest().setAttribute("pageResult", pageResult);
		getRequest().setAttribute("subjectTitle", subject.getSubjectTitle());
		return "list";
	}

	public String input() {
		List<Course> courses = courseService.getAll();
		getRequest().setAttribute("courses", courses);
		return "input";
	}

	/**
	 * 批量导入试题
	 * 
	 * @return
	 */
	public String inputBatch() {
		List<Course> courses = courseService.getAll();
		getRequest().setAttribute("courses", courses);
		return "inputBatch";
	}

	/**
	 * 新增试题信息
	 * 
	 * @return
	 */
	public String add() {
		try {

			if ("3".equals(subject.getSubjectType())) {
				String[] answer = null;
				String key = "";
				answer = getRequest().getParameterValues("answer");
				if (answer != null && answer.length > 0) {
					for (String string : answer) {
						key += string;
					}
					subject.setSubjectAnswer(key);
				}
				String optionA = getRequest().getParameter("optionA");
				String optionB = getRequest().getParameter("optionB");
				String optionC = getRequest().getParameter("optionC");
				String optionD = getRequest().getParameter("optionD");
				subject.setSubjectOptionA(optionA);
				subject.setSubjectOptionB(optionB);
				subject.setSubjectOptionC(optionC);
				subject.setSubjectOptionD(optionD);
			}
			subjectService.add(subject);
			getSession().setAttribute("msg", "新增试题成功!");
		} catch (Exception e) {
			e.printStackTrace();
			getSession().setAttribute("msg", "新增试题失败：" + e.getMessage());
		}

		return SUCCESS;
	}

	/**
	 * 修改试题信息
	 * 
	 * @return
	 */
	public String edit() {
		subject = subjectService.getById(id);
		List<Course> courses = courseService.getAll();
		getRequest().setAttribute("courses", courses);
		getRequest().setAttribute("subject", subject);
		return "edit";
	}

	/**
	 * 查询试题信息
	 * 
	 * @return
	 */
	public String view() {
		subject = subjectService.getById(id);
		getRequest().setAttribute("subject", subject);
		return "view";
	}

	/**
	 * 更新试题信息
	 * 
	 * @return
	 */
	public String update() {
		try {
			subjectService.edit(subject);
			this.addActionMessage("修改试题成功!");
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage("修改试题失败：" + e.getMessage());
		}
		return "edit";
	}

	/**
	 * 删除试题信息
	 * 
	 * @return
	 */
	public String delete() {
		try {
			subjectService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String addBatch(){
		try {
			int num = subjectService.addBatch(myFile,subject);
			getSession().setAttribute("msg", "批量新增试题成功,总共批量导入"+num+"条记录");
		} catch (Exception e) {
			e.printStackTrace();
			getSession().setAttribute("msg", "批量新增试题失败：" + e.getMessage());	
		}
		return SUCCESS;
	}
	@Override
	public Subject getModel() {
		return subject;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

}
